public class MobileAppHomepageController{
    public List<AggregateResult> applicationTypeLeftList {get; set;}
    public List<AggregateResult> applicationTypeRightList {get; set;}
    public String applicationPackage {get; set;}
    public Account currentAccount {get;set;}
    
    public MobileAppHomepageController() {
        Integer index = 0;
        applicationPackage = 'Service Letter';
        
        applicationTypeLeftList = new List<AggregateResult>();
        applicationTypeRightList = new List<AggregateResult>();
        
        for(AggregateResult result : [SELECT Application_Type__c 
                                      FROM Transaction_Types__c 
                                      WHERE isMobile__c = True AND Active__c = True 
                                      AND Application_Package__c = :applicationPackage 
                                      GROUP BY Application_Type__c
                                      ORDER BY Application_Type__c])
        {
            if (System.Math.mod(index, 2) == 0)
                applicationTypeLeftList.add(result);
            else
                applicationTypeRightList.add(result);
            
            index++;
        }
        
        
        currentAccount = new Account();
        
        List<Contact> contactList = new List<Contact> 
                                            ([SELECT Id, Name, AccountId 
                                             FROM Contact 
                                             WHERE Id IN (Select ContactId FROM User WHERE Id = :UserInfo.getUserId())
                                             LIMIT 1]);
        
        Contact currentContact = null;
        
        if (contactList.size() > 0)
            currentContact = contactList[0];
        else
            currentContact = new Contact();
        
        List<Account> accountsList = new List<Account>([SELECT Id, Name, Company_Status__c
                        FROM Account WHERE Id = :currentContact.AccountId LIMIT 1]);
        
        if(accountsList.size() > 0)
            currentAccount = accountsList[0];
    }
    
    // Code we will invoke on page load.
    public PageReference forwardToCustomAuthPage()
    {
        if(UserInfo.getUserType() == 'Guest')
        {
            return new PageReference('/MobileAppCustomLogin');
        }
        else
        {
            String userAgent = System.currentPageReference().getHeaders().get('User-Agent');
            system.debug('## userAgent => ' + userAgent);
            if(userAgent.contains('iPhone') || userAgent.contains('iPad')) {
                if(currentAccount.Company_Status__c != 'Valid') {
                    return new PageReference('/MobileAppCompanyNotValidError');
                }  else {
                   return null;
                }
            } else {
                return new PageReference('/MobileAppDeviceNotValid');
            }
        }
    }
}